home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / hm--html-menus / html-mode.el < prev    next >
Encoding:
Text File  |  1995-08-02  |  33.9 KB  |  958 lines

  1. ;;; html-mode --- Major mode for editing HTML hypertext documents for the WWW
  2. ;; Derived from Marc Andreesen's Revision 2.3.
  3.  
  4. ;; Keywords: hypermedia languages help docs wp
  5.  
  6. ;; HTML mode, based on text mode.
  7. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  8. ;; Copyright (C) 1992, 1993 National Center for Supercomputing Applications.
  9. ;; NCSA modifications by Marc Andreessen (marca@ncsa.uiuc.edu).
  10. ;;
  11. ;; Changed by Heiko Mⁿnkel, 6 Jan 1994, 10 Jan 1994, 15 Mar 1994, 03 Jan 1995
  12. ;;                12 May 1995, 25 May 1995, 29 Jul 1995
  13. ;;
  14. ;; This program is free software; you can redistribute it and/or
  15. ;; modify it under the terms of the GNU General Public License as
  16. ;; published by the Free Software Foundation; either version 1, or
  17. ;; (at your option) any later version.
  18. ;;
  19. ;; This program is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  22. ;; General Public License for more details.
  23. ;;
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with GNU Emacs; see the file COPYING.  If not, write to the
  26. ;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  27. ;;
  28. ;;; Commentary:
  29. ;; -------------------------------- CONTENTS --------------------------------
  30. ;;
  31. ;; html-mode: Major mode for editing HTML hypertext documents.
  32. ;; Revision: 2.2
  33. ;;
  34. ;; Changes from 2.1 (beta)
  35. ;;   - Changed previewer for the new Mosaic
  36. ;;   - Changed lemacs to xemacs
  37. ;;
  38. ;; Changes from 2.0 (beta):
  39. ;;   - Ripped out numeric anchor name stuff altogether (all names should be
  40. ;;     meaningful, not just numbers).
  41. ;;   - Fixed problem with unquoted names.
  42. ;;   - Fixed font-lock support (yeah! thanks lamour@engin.umich.edu).
  43. ;;
  44. ;; ------------------------------ INSTRUCTIONS ------------------------------
  45. ;;
  46. ;; Put the following code in your .emacs file:
  47. ;;
  48. ;; (autoload 'html-mode "html-mode" "HTML major mode." t)
  49. ;; (or (assoc "\\.html$" auto-mode-alist)
  50. ;;   (setq auto-mode-alist (cons '("\\.html$" . html-mode) 
  51. ;;                               auto-mode-alist)))
  52. ;;
  53. ;; Emacs will detect the ``.html'' suffix and activate html-mode
  54. ;; appropriately.
  55. ;;
  56. ;; You are assumed to be at least somewhat familiar with the HTML
  57. ;; format.  If you aren't, read about it first (see below).
  58. ;;
  59. ;; Here are key sequences and corresponding commands:
  60. ;;
  61. ;; NORMAL COMMANDS:
  62. ;;
  63. ;; C-c a         html-add-address
  64. ;;   Open an address element.
  65. ;;
  66. ;; C-c b         html-add-blockquote
  67. ;;
  68. ;; C-c C-b       html-add-bold
  69. ;;   Open a bold element.
  70. ;;
  71. ;; C-c c         html-add-code
  72. ;;   Open a 'code' (fixed-font) element.
  73. ;;
  74. ;; C-c C-c       html-add-citation
  75. ;;
  76. ;; C-c d         html-add-description-list
  77. ;;   Open a definition list.  The initial entry is created for you.
  78. ;;   To create subsequent entries, use 'C-c e'.
  79. ;;
  80. ;; C-c e         html-add-description-entry
  81. ;;   Add a new definition entry in a definition list.  You are
  82. ;;   assumed to be inside a definition list (specifically, at the end
  83. ;;   of another definition entry).
  84. ;;
  85. ;; C-c C-e       html-add-emphasized
  86. ;;   Open an emphasized element.
  87. ;;
  88. ;; C-c C-f       html-add-fixed
  89. ;;
  90. ;; C-c g         html-add-img
  91. ;;   Add an IMG element (inlined image or graphic).  Note that the
  92. ;;   IMG tag is currently an extension to HTML supported only by the
  93. ;;   NCSA Mosaic browser (to my knowledge).  You will be prompted for
  94. ;;   the URL of the image you wish to inline into the document.
  95. ;;
  96. ;; C-c h         html-add-header
  97. ;;   Add a header.  You are prompted for size (1 is biggest, 2 is
  98. ;;   next biggest; bottom limit is 6) and header contents.
  99. ;;
  100. ;; C-c i         html-add-list-or-menu-item
  101. ;;   Add a new list or menu item in a list or menu.  You are assumed
  102. ;;   to be inside a list or menu (specifically, at the end of another
  103. ;;   item).
  104. ;;
  105. ;; C-c C-i       html-add-italic
  106. ;;   Open an italic element.
  107. ;;
  108. ;; C-c C-k       html-add-keyboard
  109. ;;
  110. ;; C-c l         html-add-normal-link
  111. ;;   Add a link.  You will be prompted for the link (any string;
  112. ;;   e.g., http://foo.bar/argh/blagh).  The cursor will be left where
  113. ;;   you can type the text that will represent the link in the
  114. ;;   document.
  115. ;;
  116. ;; C-c C-l       html-add-listing
  117. ;;
  118. ;; C-c m         html-add-menu
  119. ;;   Open a menu.  The initial item is created for you.  To create
  120. ;;   additional items, use 'C-c i'.
  121. ;;
  122. ;; C-c C-m       html-add-sample
  123. ;;
  124. ;; C-c n         html-add-numbered-list
  125. ;;
  126. ;; C-c p         html-add-paragraph-separator
  127. ;;   Use this command at the end of each paragraph.
  128. ;; 
  129. ;; C-c C-p       html-add-preformatted
  130. ;;
  131. ;; C-c r         html-add-normal-reference
  132. ;;
  133. ;; C-c s         html-add-list
  134. ;;   Open a list.  The initial item is created for you.  To create
  135. ;;   additional items, use 'C-c i'.
  136. ;;
  137. ;; C-c C-s       html-add-strong
  138. ;;
  139. ;; C-c t         html-add-title
  140. ;;   Add a title to the document.  You will be prompted for the
  141. ;;   contents of the title.  If a title already exists at the very
  142. ;;   top of the document, the existing contents will be replaced.
  143. ;;
  144. ;; C-c C-v       html-add-variable
  145. ;;
  146. ;; C-c x         html-add-plaintext
  147. ;;   Add plaintext.  The cursor will be positioned where you can type
  148. ;;   plaintext (or insert another file, or whatever).
  149. ;;
  150. ;; C-c z         html-preview-document
  151. ;;   Fork off a Mosaic process to preview the current document.
  152. ;;   After you do this once, subsequent invocations of
  153. ;;   html-preview-document will cause the same Mosaic process to be
  154. ;;   used; this magic is accomplished through Mosaic's ability to be
  155. ;;   remote-controlled via Unix signals.  This feature is only
  156. ;;   available when running XEmacs v19 (it will maybe work with
  157. ;;   GNU Emacs v19; I'm not sure).
  158. ;;
  159. ;; COMMANDS THAT OPERATE ON THE CURRENT REGION:
  160. ;;
  161. ;; C-c C-r l     html-add-normal-link-to-region
  162. ;;   Add a link that will be represented by the current region.  You
  163. ;;   will be prompted for the link (any string, as with
  164. ;;   html-add-normal-link).
  165. ;;
  166. ;; C-c C-r r     html-add-reference-to-region
  167. ;;   Add a reference (a link that does not reference anything) that
  168. ;;   will be represented by the current region.  You will be prompted
  169. ;;   for the name of the link.
  170. ;;
  171. ;; SPECIAL COMMANDS:
  172. ;;
  173. ;; <, >, &
  174. ;;   These are overridden to output <, >, and &
  175. ;;   respectively.  The real characters <, >, and & can be entered
  176. ;;   into the text either by typing 'C-c' before typing the character
  177. ;;   or by using the Emacs quoted-insert (C-q) command.
  178. ;;
  179. ;; C-c <, C-c >, C-c &
  180. ;;   See '<, >, &' above.
  181. ;;
  182. ;; ---------------------------- ADDITIONAL NOTES ----------------------------
  183. ;;
  184. ;; If you are running Epoch or XEmacs, highlighting will be used
  185. ;; to deemphasize HTML message elements as they are created.  You can
  186. ;; turn this off; see the variables 'html-use-highlighting' and 
  187. ;; 'html-use-font-lock'.
  188. ;;
  189. ;; HREF and NAME arguments in anchors should always be quoted.  In
  190. ;; some existing HTML documents, they are not.  html-mode will
  191. ;; automatically quotify all such unquoted arguments when it
  192. ;; encounters them.  The following variables affect this behavior.
  193. ;;
  194. ;; html-quotify-hrefs-on-find       (variable, default t)
  195. ;;   If this is non-nil, all HREF arguments will be quotified
  196. ;;   automatically when a HTML document is loaded into Emacs
  197. ;;   (actually when html-mode is entered).
  198. ;;
  199. ;; -------------------------------- GOTCHAS ---------------------------------
  200. ;;
  201. ;; HTML documents can be tricky.  html-mode is not smart enough to
  202. ;; enforce correctness or sanity, so you have to do that yourself.
  203. ;;
  204. ;; ------------------------- WHAT HTML-MODE IS NOT --------------------------
  205. ;;
  206. ;; html-mode is not a mode for *browsing* HTML documents.  In
  207. ;; particular, html-mode provides no hypertext or World Wide Web
  208. ;; capabilities.
  209. ;;
  210. ;; The World Wide Web browser we (naturally) recommend is NCSA
  211. ;; Mosaic, which can be found at ftp.ncsa.uiuc.edu in /Mosaic.
  212. ;;
  213. ;; See file://moose.cs.indiana.edu/pub/elisp/w3 for w3.el, which is
  214. ;; an Elisp World Wide Web browser written by William Perry.
  215. ;;
  216. ;; ------------------------------ WHAT HTML IS ------------------------------
  217. ;;
  218. ;; HTML (HyperText Markup Language) is a format for hypertext
  219. ;; documents, particularly in the World Wide Web system.  For more
  220. ;; information on HTML, telnet to info.cern.ch or pick up a copy of
  221. ;; NCSA Mosaic for the X Window System via ftp to ftp.ncsa.uiuc.edu
  222. ;; in /Mosaic; information is available online through the software
  223. ;; products distributed at those sites.
  224. ;;
  225. ;; ---------------------------- ACKNOWLEDGEMENTS ----------------------------
  226. ;;
  227. ;; Some code herein provided by:
  228. ;;   Dan Connolly <connolly@pixel.convex.com>
  229. ;;
  230. ;; --------------------------------------------------------------------------
  231. ;; LCD Archive Entry:
  232. ;; html-mode|Marc Andreessen|marca@ncsa.uiuc.edu|
  233. ;; Major mode for editing HTML hypertext files.|
  234. ;; Date: sometime in 1993|Revision: 2.1 (beta)|~/modes/html-mode.el.Z|
  235. ;; --------------------------------------------------------------------------
  236. ;;; Code:
  237.  
  238. ;; XEmacs change -- we require hm--html-menu here so that we do not
  239. ;; have to manually add an autoload for html-mode.  If we didn't do
  240. ;; this the autoload for html-mode would have to be changed to load
  241. ;; hm--html-menu even though it is defined in this file.
  242. (require 'hm--html-menu)
  243.  
  244. ;;; ---------------------------- emacs variations ----------------------------
  245.  
  246. (defvar html-running-xemacs (if (or (string-match "XEmacs" emacs-version)
  247.                     (string-match "Lucid" emacs-version) )
  248.                 t nil)
  249.   "Non-nil if running XEmacs.")
  250.  
  251. (defvar html-running-epoch (boundp 'epoch::version)
  252.   "Non-nil if running Epoch.")
  253.  
  254. (defvar html-running-emacs-19 (and 
  255.                    (not html-running-xemacs)
  256.                    (string= (substring emacs-version 0 2) "19"))
  257.   "Non-nil if running Emacs 19")
  258.  
  259. ;;; ------------------------------- variables --------------------------------
  260.  
  261. (defvar html-quotify-hrefs-on-find t
  262.   "*If non-nil, all HREF's (and NAME's) in a file will be automatically 
  263. quotified when the file is loaded.  This is useful for converting ancient 
  264. HTML documents to SGML-compatible syntax, which mandates quoted HREF's.
  265. This should always be T.")
  266.  
  267. (defvar html-use-highlighting html-running-epoch
  268.   "*Flag to use highlighting for HTML directives in Epoch or XEmacs; 
  269. if non-NIL, highlighting will be used.  Default is T if you are running
  270. Epoch; nil otherwise (for XEmacs, font-lock is better; see 
  271. html-use-font-lock instead).")
  272.  
  273. (defvar html-use-font-lock (or html-running-xemacs html-running-emacs-19)
  274.   "*Flag to use font-lock for HTML directives in XEmacs.  If non-NIL,
  275. font-lock will be used.  Default is T if you are running with XEmacs;
  276. NIL otherwise.  This doesn't currently seem to work.  Bummer.  Ten points
  277. to the first person who tells me why not.")
  278.  
  279. (defvar html-deemphasize-color "grey80"
  280.   "*Color for de-highlighting HTML directives in Epoch or XEmacs.")
  281.  
  282. (defvar html-emphasize-color "yellow"
  283.   "*Color for highlighting HTML something-or-others in Epoch or XEmacs.")
  284.  
  285. (defvar html-document-previewer "xmosaic"
  286.   "*Program to be used to preview HTML documents.  Program is assumed
  287. to accept a single argument, a filename containing a file to view; program
  288. is also assumed to follow the Mosaic convention of handling SIGUSR1 as
  289. a remote-control mechanism.")
  290.  
  291. (defvar html-document-previewer-args "-ngh"
  292.   "*Arguments to be given to the program named by html-document-previewer;
  293. NIL if none should be given.")
  294.  
  295. (defvar html-sigusr1-signal-value 16
  296.   "*Value for the SIGUSR1 signal on your system.  See, usually,
  297. /usr/include/sys/signal.h.")
  298.  
  299. ;;; --------------------------------- setup ----------------------------------
  300.  
  301. (defvar html-mode-syntax-table nil
  302.   "Syntax table used while in html mode.")
  303.  
  304. (defvar html-mode-abbrev-table nil
  305.   "Abbrev table used while in html mode.")
  306. (define-abbrev-table 'html-mode-abbrev-table ())
  307.  
  308. (if html-mode-syntax-table
  309.     ()
  310.   (setq html-mode-syntax-table (make-syntax-table))
  311.   (modify-syntax-entry ?\" ".   " html-mode-syntax-table)
  312.   (modify-syntax-entry ?\\ ".   " html-mode-syntax-table)
  313.   (modify-syntax-entry ?' "w   " html-mode-syntax-table))
  314.  
  315. (defvar html-mode-map nil "")
  316. (if html-mode-map
  317.     ()
  318.   (setq html-mode-map (make-sparse-keymap))
  319.   (define-key html-mode-map "\t" 'tab-to-tab-stop)
  320.   (define-key html-mode-map "\C-ca" 'html-add-address)
  321.   (define-key html-mode-map "\C-cb" 'html-add-blockquote)
  322.   (define-key html-mode-map "\C-cc" 'html-add-code)
  323.   (define-key html-mode-map "\C-cd" 'html-add-description-list)
  324.   (define-key html-mode-map "\C-ce" 'html-add-description-entry)
  325.   (define-key html-mode-map "\C-cg" 'html-add-img)
  326.   (define-key html-mode-map "\C-ch" 'html-add-header)
  327.   (define-key html-mode-map "\C-ci" 'html-add-list-or-menu-item)
  328.   (define-key html-mode-map "\C-cl" 'html-add-normal-link)
  329.   (define-key html-mode-map "\C-cm" 'html-add-menu)
  330.   (define-key html-mode-map "\C-cn" 'html-add-numbered-list)
  331.   (define-key html-mode-map "\C-cp" 'html-add-paragraph-separator)
  332.   (define-key html-mode-map "\C-cr" 'html-add-normal-reference)
  333.   (define-key html-mode-map "\C-cs" 'html-add-list)
  334.   (define-key html-mode-map "\C-ct" 'html-add-title)
  335.   (define-key html-mode-map "\C-cx" 'html-add-plaintext)
  336.   ;; html-preview-document currently requires the primitive
  337.   ;; signal-process, which is only in v19 (is it in gnu 19? dunno).
  338.   (and html-running-xemacs
  339.        (define-key html-mode-map "\C-cz" 'html-preview-document))
  340.   (define-key html-mode-map "\C-c\C-b" 'html-add-bold)
  341.   (define-key html-mode-map "\C-c\C-c" 'html-add-citation)
  342.   (define-key html-mode-map "\C-c\C-e" 'html-add-emphasized)
  343.   (define-key html-mode-map "\C-c\C-f" 'html-add-fixed)
  344.   (define-key html-mode-map "\C-c\C-i" 'html-add-italic)
  345.   (define-key html-mode-map "\C-c\C-k" 'html-add-keyboard)
  346.   (define-key html-mode-map "\C-c\C-l" 'html-add-listing)
  347.   (define-key html-mode-map "\C-c\C-m" 'html-add-sample)
  348.   (define-key html-mode-map "\C-c\C-p" 'html-add-preformatted)
  349.   (define-key html-mode-map "\C-c\C-s" 'html-add-strong)
  350.   (define-key html-mode-map "\C-c\C-v" 'html-add-variable)
  351.   (define-key html-mode-map "<" 'html-less-than)
  352.   (define-key html-mode-map ">" 'html-greater-than)
  353.   (define-key html-mode-map "&" 'html-ampersand)
  354.   (define-key html-mode-map "\C-c<" 'html-real-less-than)
  355.   (define-key html-mode-map "\C-c>" 'html-real-greater-than)
  356.   (define-key html-mode-map "\C-c&" 'html-real-ampersand)
  357.   (define-key html-mode-map "\C-c\C-rl" 'html-add-normal-link-to-region)
  358.   (define-key html-mode-map "\C-c\C-rr" 'html-add-reference-to-region)
  359. )
  360.  
  361. ;;; ------------------------------ highlighting ------------------------------
  362.  
  363. (if (and html-running-epoch html-use-highlighting)
  364.     (progn
  365.       (defvar html-deemphasize-style (make-style))
  366.       (set-style-foreground html-deemphasize-style html-deemphasize-color)
  367.       (defvar html-emphasize-style (make-style))
  368.       (set-style-foreground html-emphasize-style html-emphasize-color)))
  369.  
  370. (if (and html-running-xemacs html-use-highlighting)
  371.     (progn
  372.       (defvar html-deemphasize-style (make-face 'html-deemphasize-face))
  373.       (set-face-foreground html-deemphasize-style html-deemphasize-color)
  374.       (defvar html-emphasize-style (make-face 'html-emphasize-face))
  375.       (set-face-foreground html-emphasize-style html-emphasize-color)))
  376.  
  377. (if html-use-highlighting
  378.     (progn
  379.       (if html-running-xemacs
  380.           (defun html-add-zone (start end style)
  381.             "Add a XEmacs extent from START to END with STYLE."
  382.             (let ((extent (make-extent start end)))
  383.               (set-extent-face extent style)
  384.               (set-extent-data extent 'html-mode))))
  385.       (if html-running-epoch
  386.           (defun html-add-zone (start end style)
  387.             "Add an Epoch zone from START to END with STYLE."
  388.             (let ((zone (add-zone start end style)))
  389.               (epoch::set-zone-data zone 'html-mode))))))
  390.  
  391. (defun html-maybe-deemphasize-region (start end)
  392.   "Maybe deemphasize a region of text.  Region is from START to END."
  393.   (and (or html-running-epoch html-running-xemacs)
  394.        html-use-highlighting
  395.        (html-add-zone start end html-deemphasize-style)))
  396.  
  397. ;;; --------------------------------------------------------------------------
  398. ;;; ------------------------ command support routines ------------------------
  399. ;;; --------------------------------------------------------------------------
  400.  
  401. (defun html-add-link (link-object)
  402.   "Add a link.  Single argument LINK-OBJECT is value of HREF in the
  403. new anchor.  Mark is set after anchor."
  404.   (let ((start (point)))
  405.     (insert "<A")
  406.     (insert " HREF=\"" link-object "\">")
  407.     (html-maybe-deemphasize-region start (1- (point)))
  408.     (insert "</A>")
  409.     (push-mark)
  410.     (forward-char -4)
  411.     (html-maybe-deemphasize-region (1+ (point)) (+ (point) 4))))
  412.  
  413. (defun html-add-reference (ref-object)
  414.   "Add a reference.  Single argument REF-OBJECT is value of NAME in the
  415. new anchor.  Mark is set after anchor."
  416.   (let ((start (point)))
  417.     (insert "<A")
  418.     (insert " NAME=\"" ref-object "\">")
  419.     (html-maybe-deemphasize-region start (1- (point)))
  420.     (insert "</A>")
  421.     (push-mark)
  422.     (forward-char -4)
  423.     (html-maybe-deemphasize-region (1+ (point)) (+ (point) 4))))
  424.  
  425. (defun html-add-list-internal (type)
  426.   "Set up a given type of list by opening the list start/end pair
  427. and creating an initial element.  Single argument TYPE is a string,
  428. assumed to be a valid HTML list type (e.g. \"UL\" or \"OL\").
  429. Mark is set after list."
  430.   (let ((start (point)))
  431.     (insert "<" type ">\n")
  432.     (html-maybe-deemphasize-region start (1- (point)))
  433.     (insert "<LI> ")
  434.     ;; Point goes right there.
  435.     (save-excursion
  436.       (insert "\n")
  437.       (setq start (point))
  438.       (insert "</" type ">\n")
  439.       (html-maybe-deemphasize-region start (1- (point)))
  440.       ;; Reuse start to set mark.
  441.       (setq start (point)))
  442.     (push-mark start t)))
  443.  
  444. (defun html-open-area (tag)
  445.   "Open an area for entering text such as PRE, XMP, or LISTING."
  446.   (let ((start (point)))
  447.     (insert "<" tag ">\n")
  448.     (html-maybe-deemphasize-region start (1- (point)))
  449.     (save-excursion
  450.       (insert "\n")
  451.       (setq start (point))
  452.       (insert "</" tag ">\n")
  453.       (html-maybe-deemphasize-region start (1- (point)))
  454.       ;; Reuse start to set mark.
  455.       (setq start (point)))
  456.     (push-mark start t)))
  457.  
  458. (defun html-open-field (tag)
  459.   (let ((start (point)))
  460.     (insert "<" tag ">")
  461.     (html-maybe-deemphasize-region start (1- (point)))
  462.     (setq start (point))
  463.     (insert "</" tag ">")
  464.     (html-maybe-deemphasize-region (1+ start) (point))
  465.     (push-mark)
  466.     (goto-char start)))
  467.  
  468. ;;; --------------------------------------------------------------------------
  469. ;;; -------------------------------- commands --------------------------------
  470. ;;; --------------------------------------------------------------------------
  471.  
  472. ;; C-c a
  473. (defun html-add-address ()
  474.   "Add an address."
  475.   (interactive)
  476.   (html-open-field "ADDRESS"))
  477.  
  478. ;; C-c b
  479. (defun html-add-blockquote ()
  480.   (interactive)
  481.   (html-open-area "BLOCKQUOTE"))
  482.  
  483. ;; C-c C-b
  484. (defun html-add-bold ()
  485.   (interactive)
  486.   (html-open-field "B"))
  487.  
  488. ;; C-c c
  489. (defun html-add-code ()
  490.   (interactive)
  491.   (html-open-field "CODE"))
  492.  
  493. ;; C-c C-c
  494. (defun html-add-citation ()
  495.   (interactive)
  496.   (html-open-field "CITE"))
  497.  
  498. ;; C-c d
  499. (defun html-add-description-list ()
  500.   "Add a definition list.  Blah blah."
  501.   (interactive)
  502.   (let ((start (point)))
  503.     (insert "<DL>\n")
  504.     (html-maybe-deemphasize-region start (1- (point)))
  505.     (insert "<DT> ")
  506.     ;; Point goes right there.
  507.     (save-excursion
  508.       (insert "\n<DD> \n")
  509.       (setq start (point))
  510.       (insert "</DL>\n")
  511.       (html-maybe-deemphasize-region start (1- (point)))
  512.       ;; Reuse start to set mark.
  513.       (setq start (point)))
  514.     (push-mark start t)))
  515.  
  516. ;; C-c e
  517. (defun html-add-description-entry ()
  518.   "Add a definition entry.  Assume we're at the end of a previous
  519. entry."
  520.   (interactive)
  521.   (let ((start (point)))
  522.     (insert "\n<DT> ")
  523.     (save-excursion
  524.       (insert "\n<DD> "))))
  525.  
  526. ;; C-c C-e
  527. (defun html-add-emphasized ()
  528.   (interactive)
  529.   (html-open-field "EM"))
  530.  
  531. ;; C-c C-f
  532. (defun html-add-fixed ()
  533.   (interactive)
  534.   (html-open-field "TT"))
  535.  
  536. ;; C-c g
  537. (defun html-add-img (href)
  538.   "Add an img."
  539.   (interactive "sImage URL: ")
  540.   (let ((start (point)))
  541.     (insert "<IMG SRC=\"" href "\">")
  542.     (html-maybe-deemphasize-region (1+ start) (1- (point)))))
  543.  
  544. ;; C-c h
  545. (defun html-add-header (size header)
  546.   "Add a header."
  547.   (interactive "sSize (1-6; 1 biggest): \nsHeader: ")
  548.   (let ((start (point)))
  549.     (insert "<H" size ">")
  550.     (html-maybe-deemphasize-region start (1- (point)))
  551.     (insert header)
  552.     (setq start (point))
  553.     (insert "</H" size ">\n")
  554.     (html-maybe-deemphasize-region (1+ start) (1- (point)))))
  555.  
  556. ;; C-c i
  557. (defun html-add-list-or-menu-item ()
  558.   "Add a list or menu item.  Assume we're at the end of the
  559. last item."
  560.   (interactive)
  561.   (let ((start (point)))
  562.     (insert "\n<LI> ")))
  563.  
  564. ;; C-c C-i
  565. (defun html-add-italic ()
  566.   (interactive)
  567.   (html-open-field "I"))
  568.  
  569. ;; C-c C-k
  570. (defun html-add-keyboard ()
  571.   (interactive)
  572.   (html-open-field "KBD"))
  573.  
  574. ;; C-c l
  575. (defun html-add-normal-link (link)
  576.   "Make a link"
  577.   (interactive "sLink to: ")
  578.   (html-add-link link))
  579.  
  580. ;; C-c C-l
  581. (defun html-add-listing ()
  582.   (interactive)
  583.   (html-open-area "LISTING"))
  584.  
  585. ;; C-c m
  586. (defun html-add-menu ()
  587.   "Add a menu."
  588.   (interactive)
  589.   (html-add-list-internal "MENU"))
  590.  
  591. ;; C-c C-m
  592. (defun html-add-sample ()
  593.   (interactive)
  594.   (html-open-field "SAMP"))
  595.  
  596. ;; C-c n
  597. (defun html-add-numbered-list ()
  598.   "Add a numbered list."
  599.   (interactive)
  600.   (html-add-list-internal "OL"))
  601.  
  602. ;; C-c p
  603. (defun html-add-paragraph-separator ()
  604.   "Add a paragraph separator."
  605.   (interactive)
  606.   (let ((start (point)))
  607.     (insert " <P>")
  608.     (html-maybe-deemphasize-region (+ start 1) (point))))
  609.  
  610. ;; C-c C-p
  611. (defun html-add-preformatted ()
  612.   (interactive)
  613.   (html-open-area "PRE"))
  614.  
  615. ;; C-c r
  616. (defun html-add-normal-reference (reference)
  617.   "Add a reference (named anchor)."
  618.   (interactive "sReference name: ")
  619.   (html-add-reference reference))
  620.  
  621. ;; C-c s
  622. (defun html-add-list ()
  623.   "Add a list."
  624.   (interactive)
  625.   (html-add-list-internal "UL"))
  626.  
  627. ;; C-c C-s
  628. (defun html-add-strong ()
  629.   (interactive)
  630.   (html-open-field "STRONG"))
  631.  
  632. ;; C-c t
  633. (defun html-add-title (title)
  634.   "Add or modify a title."
  635.   (interactive "sTitle: ")
  636.   (save-excursion
  637.     (goto-char (point-min))
  638.     (if (and (looking-at "<TITLE>")
  639.              (save-excursion
  640.                (forward-char 7)
  641.                (re-search-forward "[^<]*" 
  642.                                   (save-excursion (end-of-line) (point)) 
  643.                                   t)))
  644.         ;; Plop the new title in its place.
  645.         (replace-match title t)
  646.       (insert "<TITLE>")
  647.       (html-maybe-deemphasize-region (point-min) (1- (point)))
  648.       (insert title)
  649.       (insert "</TITLE>")
  650.       (html-maybe-deemphasize-region (- (point) 7) (point))
  651.       (insert "\n"))))
  652.  
  653. ;; C-c C-v
  654. (defun html-add-variable ()
  655.   (interactive)
  656.   (html-open-field "VAR"))
  657.  
  658. ;; C-c x
  659. (defun html-add-plaintext ()
  660.   "Add plaintext."
  661.   (interactive)
  662.   (html-open-area "XMP"))
  663.  
  664. ;;; --------------------------------------------------------------------------
  665. ;;; ---------------------------- region commands -----------------------------
  666. ;;; --------------------------------------------------------------------------
  667.  
  668. ;; C-c C-r l
  669. (defun html-add-normal-link-to-region (link start end)
  670.   "Make a link that applies to the current region.  Again,
  671. no completion."
  672.   (interactive "sLink to: \nr")
  673.   (save-excursion
  674.     (goto-char end)
  675.     (save-excursion
  676.       (goto-char start)
  677.       (insert "<A")
  678.       (insert " HREF=\"" link "\">")
  679.       (html-maybe-deemphasize-region start (1- (point))))
  680.     (insert "</A>")
  681.     (html-maybe-deemphasize-region (- (point) 3) (point))))
  682.  
  683. ;; C-c C-r r
  684. (defun html-add-reference-to-region (name start end)
  685.   "Add a reference point (a link with no reference of its own) to
  686. the current region."
  687.   (interactive "sName: \nr")
  688.   (or (string= name "")
  689.       (save-excursion
  690.         (goto-char end)
  691.         (save-excursion
  692.           (goto-char start)
  693.           (insert "<A NAME=\"" name "\">")
  694.           (html-maybe-deemphasize-region start (1- (point))))
  695.         (insert "</A>")
  696.         (html-maybe-deemphasize-region (- (point) 3) (point)))))
  697.  
  698. ;;; --------------------------------------------------------------------------
  699. ;;; ---------------------------- special commands ----------------------------
  700. ;;; --------------------------------------------------------------------------
  701.  
  702. (defun html-less-than ()
  703.   (interactive)
  704.   (insert "<"))
  705.  
  706. (defun html-greater-than ()
  707.   (interactive)
  708.   (insert ">"))
  709.  
  710. (defun html-ampersand ()
  711.   (interactive)
  712.   (insert "&"))
  713.  
  714. (defun html-real-less-than ()
  715.   (interactive)
  716.   (insert "<"))
  717.  
  718. (defun html-real-greater-than ()
  719.   (interactive)
  720.   (insert ">"))
  721.  
  722. (defun html-real-ampersand ()
  723.   (interactive)
  724.   (insert "&"))
  725.  
  726. ;;; --------------------------------------------------------------------------
  727. ;;; --------------------------- Mosaic previewing ----------------------------
  728. ;;; --------------------------------------------------------------------------
  729.  
  730. ;; OK, we work like this: We have a variable html-previewer-process.
  731. ;; When we start, it's nil.  First time html-preview-document is
  732. ;; called, we write the current document into a tmp file and call
  733. ;; Mosaic on it.  Second time html-preview-document is called, we
  734. ;; write the current document into a tmp file, write out a tmp config
  735. ;; file, and send Mosaic SIGUSR1.
  736.  
  737. ;; This feature REQUIRES the Lisp command signal-process, which seems
  738. ;; to be a XEmacs v19 feature.  It might be in GNU Emacs v19 too;
  739. ;; I dunno.
  740.  
  741. (defvar html-previewer-process nil
  742.   "Variable used to track live viewer process.")
  743.  
  744. (defun html-write-buffer-to-tmp-file ()
  745.   "Write the current buffer to a temp file and return the name
  746. of the tmp file."
  747.   (let ((filename (concat "/tmp/" (make-temp-name "html") ".html")))
  748.     (write-region (point-min) (point-max) filename nil 'foo)
  749.     filename))
  750.  
  751. (defun html-preview-document ()
  752.   "Preview the current buffer's HTML document by spawning off a
  753. previewing process (assumed to be Mosaic, basically) and controlling
  754. it with signals as long as it's alive."
  755.   (interactive)
  756.   (let ((tmp-file (html-write-buffer-to-tmp-file)))
  757.     ;; If html-previewer-process is nil, we start a process.
  758.     ;; OR if the process status is not equal to 'run.
  759.     (if (or (eq html-previewer-process nil)
  760.             (not (eq (process-status html-previewer-process) 'run)))
  761.         (progn
  762.           (message "Starting previewer...")
  763.           (setq html-previewer-process
  764.                 (if html-document-previewer-args
  765.                     (start-process "html-previewer" "html-previewer"
  766.                                    html-document-previewer 
  767.                                    html-document-previewer-args 
  768.                                    tmp-file)
  769.                   (start-process "html-previewer" "html-previewer"
  770.                                  html-document-previewer 
  771.                                  tmp-file))))
  772.       ;; We've got a running previewer; use it via SIGUSR1.
  773.       (save-excursion
  774.         (let ((config-file (format "/tmp/Mosaic.%d" 
  775.                                    (process-id html-previewer-process))))
  776.           (set-buffer (generate-new-buffer "*html-preview-tmp*"))
  777.           (insert "goto\nfile:" tmp-file "\n")
  778.           (write-region (point-min) (point-max)
  779.                         config-file nil 'foo)
  780.           ;; This is a v19 routine only.
  781.           (signal-process (process-id html-previewer-process)
  782.                           html-sigusr1-signal-value)
  783.           (delete-file config-file)
  784.           (delete-file tmp-file)
  785.           (kill-buffer (current-buffer)))))))
  786.  
  787. ;;; --------------------------------------------------------------------------
  788. ;;; --------------------------------------------------------------------------
  789. ;;; --------------------------------------------------------------------------
  790.  
  791. (defun html-replace-string-in-buffer (start end newstring)
  792.   (save-excursion
  793.     (goto-char start)
  794.     (delete-char (1+ (- end start)))
  795.     (insert newstring)))
  796.  
  797. ;;; --------------------------- html-quotify-hrefs ---------------------------
  798.  
  799. (defun html-quotify-hrefs ()
  800.   "Insert quotes around all HREF and NAME attribute value literals.
  801.  
  802. This remedies the problem with old HTML files that can't be processed
  803. by SGML parsers. That is, changes <A HREF=foo> to <A HREF=\"foo\">."
  804.   (interactive)
  805.   (save-excursion
  806.     (goto-char (point-min))
  807.     (while 
  808.         (re-search-forward
  809.          "<[aA][ \t\n]+\\([nN][aA][mM][eE]=[a-zA-Z0-9]+[ \t\n]+\\)?[hH][rR][eE][fF]="
  810.          (point-max)
  811.          t)
  812.       (cond
  813.        ((null (looking-at "\""))
  814.         (insert "\"")
  815.         (re-search-forward "[ \t\n>]" (point-max) t)
  816.         (forward-char -1)
  817.         (insert "\""))))))
  818.  
  819. ;;; ------------------------------- html-mode --------------------------------
  820.  
  821. ;;;###autoload
  822. (defun html-mode ()
  823.   "Major mode for editing HTML hypertext documents.  Special commands:\\{html-mode-map}
  824. Turning on html-mode calls the value of the variable html-mode-hook,
  825. if that value is non-nil.
  826.  
  827. More extensive documentation is available in the file 'html-mode.el'.
  828. The latest (possibly unstable) version of this file will always be available
  829. on anonymous FTP server ftp.ncsa.uiuc.edu in /Mosaic/elisp."
  830.   (interactive)
  831.   (kill-all-local-variables)
  832.   (if hm--html-use-old-keymap
  833.       (use-local-map html-mode-map)
  834.     (use-local-map hm--html-mode-map))
  835.   (setq mode-name "HTML")
  836.   (setq major-mode 'html-mode)
  837.   (setq local-abbrev-table html-mode-abbrev-table)
  838.   (set-syntax-table html-mode-syntax-table)
  839.   (run-hooks 'html-mode-hook)
  840.   (and html-use-font-lock
  841.        (html-fontify)))
  842.  
  843. ;;; ------------------------------- our hooks --------------------------------
  844.  
  845. (defun html-html-mode-hook ()
  846.   "Hook called from html-mode-hook.  
  847. Run htlm-quotify-hrefs if html-quotify-hrefs-on-find is non-nil."
  848.   ;; Quotify existing HREF's if html-quotify-hrefs-on-find is non-nil.
  849.   (and html-quotify-hrefs-on-find (html-quotify-hrefs)))
  850.  
  851. ;;; ------------------------------- hook setup -------------------------------
  852.  
  853. ;; Author: Daniel LaLiberte (liberte@cs.uiuc.edu).
  854. (defun html-postpend-unique-hook (hook-var hook-function)
  855.   "Postpend HOOK-VAR with HOOK-FUNCTION, if it is not already an element.
  856. hook-var's value may be a single function or a list of functions."
  857.   (if (boundp hook-var)
  858.       (let ((value (symbol-value hook-var)))
  859.         (if (and (listp value) (not (eq (car value) 'lambda)))
  860.             (and (not (memq hook-function value))
  861.                  (set hook-var (append value (list hook-function))))
  862.           (and (not (eq hook-function value))
  863.                (set hook-var (append value (list hook-function))))))
  864.     (set hook-var (list hook-function))))
  865.  
  866. (html-postpend-unique-hook 'html-mode-hook 'html-html-mode-hook)
  867.  
  868. ;;; -------------------------- xemacs menubar setup ---------------------------
  869.  
  870. (if (or html-running-xemacs html-running-emacs-19)
  871.     (progn
  872.       (defvar html-menu
  873.         '("HTML Mode"
  874.           ["Open Address"         html-add-address      t]
  875.           ["Open Blockquote"      html-add-blockquote   t]
  876.           ["Open Header"          html-add-header       t]
  877.           ["Open Hyperlink"       html-add-normal-link  t]
  878.           ["Open Listing"         html-add-listing      t]
  879.           ["Open Plaintext"       html-add-plaintext    t]
  880.           ["Open Preformatted"    html-add-preformatted t]
  881.           ["Open Reference"       html-add-normal-reference    t]
  882.           ["Open Title"           html-add-title        t]
  883.           "----"
  884.           ["Open Bold"            html-add-bold         t]
  885.           ["Open Citation"        html-add-citation     t]
  886.           ["Open Code"            html-add-code         t]
  887.           ["Open Emphasized"      html-add-emphasized   t]
  888.           ["Open Fixed"           html-add-fixed        t]
  889.           ["Open Keyboard"        html-add-keyboard     t]
  890.           ["Open Sample"          html-add-sample       t]
  891.           ["Open Strong"          html-add-strong       t]
  892.           ["Open Variable"        html-add-variable     t]
  893.           "----"
  894.           ["Add Inlined Image"    html-add-img          t]
  895.           ["End Paragraph"        html-add-paragraph-separator t]
  896.           ["Preview Document"     html-preview-document t]
  897.           "----"
  898.           ("Definition List ..."
  899.            ["Open Definition List"    html-add-description-list  t]
  900.            ["Add Definition Entry"    html-add-description-entry t]
  901.            )
  902.           ("Other Lists ..."
  903.            ["Open Unnumbered List"    html-add-list          t]
  904.            ["Open Numbered List"      html-add-numbered-list t]
  905.            ["Open Menu"               html-add-menu          t]
  906.            "----"
  907.            ["Add List Or Menu Item"   html-add-list-or-menu-item   t]
  908.            )           
  909.           ("Operations On Region ..."
  910.            ["Add Hyperlink To Region" html-add-normal-link-to-region  t]
  911.            ["Add Reference To Region" html-add-reference-to-region    t]
  912.            )
  913.           ("Reserved Characters ..."
  914.            ["Less Than (<)"           html-real-less-than      t]
  915.            ["Greater Than (>)"        html-real-greater-than   t]
  916.            ["Ampersand (&)"           html-real-ampersand      t]
  917.            )
  918.           )
  919.         )
  920.  
  921. ;      (defun html-menu (e)
  922. ;        (interactive "e")
  923. ;        (mouse-set-point e)
  924. ;        (beginning-of-line)
  925. ;        (popup-menu html-menu))
  926.       
  927. ;      (define-key html-mode-map 'button3 'html-menu)
  928.  
  929. ;      (defun html-install-menubar ()
  930. ;        (if (and current-menubar (not (assoc "HTML" current-menubar)))
  931. ;            (progn
  932. ;              (set-buffer-menubar (copy-sequence current-menubar))
  933. ;              (add-menu nil "HTML" (cdr html-menu)))))
  934. ;      (html-postpend-unique-hook 'html-mode-hook 'html-install-menubar)
  935.  
  936.       (defvar html-font-lock-keywords
  937.         (list
  938.          '("\\(<[^>]*>\\)+" . font-lock-comment-face)
  939.          '("[Hh][Rr][Ee][Ff]=\"\\([^\"]*\\)\"" 1 font-lock-string-face t)
  940.          '("[Ss][Rr][Cc]=\"\\([^\"]*\\)\"" 1 font-lock-string-face t))
  941.         "Patterns to highlight in HTML buffers.")
  942.  
  943.       (defun html-fontify ()
  944.         (font-lock-mode 1)
  945.         (make-local-variable 'font-lock-keywords) 
  946.         (setq font-lock-keywords html-font-lock-keywords)
  947.     (font-lock-hack-keywords (point-min) (point-max))
  948.         (message "Hey boss, we been through html-fontify."))
  949.       )
  950.   )
  951.  
  952. ;;; ------------------------------ final setup -------------------------------
  953.  
  954. (or (rassq 'html-mode auto-mode-alist) ;jwz
  955.     (setq auto-mode-alist (cons '("\\.html\\'" . html-mode) auto-mode-alist)))
  956.  
  957. (provide 'html-mode)
  958.